/*-----------------------------------------------------------------------*/
/* Low level disk I/O module SKELETON for FatFs     (C)ChaN, 2019        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/

#include "ff.h"			/* Obtains integer types */
#include "diskio.h"		/* Declarations of disk functions */
#include "sfud.h"

const sfud_flash *flash_w25q64;	// W25Q64 flash pointer

/* Definitions of physical drive number for each drive */
#define ATA			    0     // SD Card
#define SPI_FLASH		1     // W25Q64 SPI Flash


/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
	DSTATUS status = STA_NOINIT;

	switch (pdrv) 
	{
		case ATA :
			// translate the reslut code here
			break;
			
		case SPI_FLASH:
			flash_w25q64 = sfud_get_device(SFUD_W25_DEVICE_INDEX);
			if (flash_w25q64->init_ok == true) {	
				status &= ~STA_NOINIT;	/* Device ID right */
			} 
			else {
				status = STA_NOINIT;	/* Device ID error */
			}
			break;

		default:
			status = STA_NOINIT;
	}	
	return status;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
	DSTATUS status = STA_NOINIT;

	switch (pdrv) 
	{
		case ATA :
			// translate the reslut code here
			break;
			
		case SPI_FLASH:
			status = disk_status(SPI_FLASH);
			break;

		default:
			status = STA_NOINIT;
	}	
	return status;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	LBA_t sector,	/* Start sector in LBA */
	UINT count		/* Number of sectors to read */
)
{
	DRESULT status = RES_PARERR;

	switch (pdrv) 
	{
		case ATA :
			// translate the reslut code here
			break;
			
		case SPI_FLASH:
			sfud_read(flash_w25q64, sector << 12, count << 12, buff);	// 4k bytes one sector
			status = RES_OK;
			break;

		default:
			status = RES_PARERR;
	}	
	return status;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
	const BYTE *buff,	/* Data to be written */
	LBA_t sector,		/* Start sector in LBA */
	UINT count			/* Number of sectors to write */
)
{
	DRESULT status = RES_PARERR;

	switch (pdrv) 
	{
		case ATA :
			// translate the reslut code here
			break;
	
		case SPI_FLASH:
			sfud_erase(flash_w25q64, sector << 12, 4096);
			sfud_write(flash_w25q64, sector << 12, count << 12, (uint8_t *)buff);
			status = RES_OK;
			break;

		default:
			status = RES_PARERR;
	}
	return status;
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
DRESULT status = RES_PARERR;
    switch (pdrv)
    {
		case ATA: /* SD CARD */
			break;

		case SPI_FLASH:
			switch (cmd)
			{
				case GET_SECTOR_COUNT:
					*(DWORD *)buff = 2048;	// (8M/4k=)2048 sectors
					break;
				case GET_SECTOR_SIZE:
					*(WORD *)buff = 4096;	// 4k bytes one sector
					break;
				case GET_BLOCK_SIZE:
					*(DWORD *)buff = 1;
					break;
			}
			status = RES_OK;
			break;

		default:
			status = RES_PARERR;
    }
    return status;
}

/*---------------------------------------------------------*/
/* User provided RTC function for FatFs module             */
/*---------------------------------------------------------*/
/* This is a real time clock service to be called back     */
/* from FatFs module.                                      */

#if !FF_FS_NORTC && !FF_FS_READONLY
DWORD get_fattime(void)
{
    return    ((DWORD)(2024 - 1980) << 25)  /* Year 2024 */
            | ((DWORD)5 << 21)         /* Month 5 */
            | ((DWORD)30 << 16)        /* Mday 30 */
            | ((DWORD)17 << 11)        /* Hour 0 */
            | ((DWORD)20 << 5)         /* Min 0 */
            | ((DWORD)0 >> 1);         /* Sec 0 */
}
#endif
